home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 12 / Mac Magazin and MacEasy Magazine CD - Issue 12.iso / Sharewarebibliothek / Anwendungen / Wissenschaft & Technik / Yorick / yorick11-fpu folder / include / multi.i < prev    next >
Text File  |  1995-04-03  |  49KB  |  1,308 lines

  1. /*
  2.    MULTI.I
  3.    Drat/DSP routines defining and implementing a standard for
  4.    retrieving opacity/emissivity data from multiple files and
  5.    combining it for a single problem.
  6.  
  7.    $Id$
  8.  */
  9.  
  10. /*
  11.    Design for a Drat interface which handles opacity/emissivity
  12.    input from several post-processing files.  Key features:
  13.  
  14.    (1) One file is the "master file", which contains all of the
  15.        zones (rt, zt, ireg from the original calculation), at all
  16.        of the times that were dumped.  This file may or may not
  17.        contain its own gb, ekap, and akap arrays.
  18.  
  19.    (2) All other files may contain only subsets of the zones or
  20.        times of the master file.  If they contain only a subset of
  21.        the times, the transport calculation will be done only at
  22.        the times which are common to every file.  Any zone may
  23.        get contributions to its opacity and emissivity from zero,
  24.        one, or more of the files.
  25.  
  26.    (3) Every file (except possibly the master file) contains its own
  27.        gb, ekap, and akap arrays.  Optionally, it may contain a gexist
  28.        array, which is one shorter than gb, and is 0 in any bins which
  29.        do not exist in that file.  The group dimension of ekap and akap
  30.        should be the number of non-zero elements of gexist.  This allows
  31.        calculations with non-contiguous coverage of photon energy.
  32.        Alternatively, codes which have no concept of photon energy bins
  33.        may write a gav array, and omit the gb array.  A suitable gb will
  34.        be inferred, and gexist is meaningless for this case.
  35.        ===>> In no case may gb or gexist (or gav) change either as a
  36.              function of time, or from zone to zone.
  37.        However, the group structures of the various files need not match.
  38.        You can either specify an overall group structure, or have the
  39.        code generate one for you before you perform the transport
  40.        calculation; in either case, the code will interpolate from
  41.        the group structure in each file onto the common group structure
  42.        where the transport calculation is performed.
  43.  
  44.    (4) Zones are determined by a separate list of "master" zone indices
  45.        for each slave file (and optionally for the master file, too).
  46.        If a zone in some slave file is marked in its zone list, that
  47.        file will contribute both opacity and emissivity to the total
  48.        for that zone, otherwise it will not contribute.
  49.  
  50.    (5) The quantities currently dumped, akap (1/length) and
  51.        ekap (power/photon energy/solid angle/area) may not be appropriate
  52.        for another opacity code, because the ekaps from several sources
  53.        cannot simply be added.  Therefore, the slave files may optionally
  54.        dump the variable emiss (power/photon energy/solid angle/volume)
  55.        instead of ekap.  The emiss and akap from several different files
  56.        simply add.
  57.  
  58.    (6) Some codes may need to dump the opacity arrays with the
  59.        frequency as the first index. The freqfirst flag handles this.
  60.  */
  61.  
  62. func multio(filename, opac=, emiss=, srcf=, oscale=, escale=,
  63.         gb=, gav=, gexist=, gscale=, zonelist=, zoneuse=,
  64.         tscale=, noextrap=, freqfirst=)
  65. /* DOCUMENT mf= multio(filename)
  66.          or mf= multio(file)
  67.      opens file FILENAME for use with the multi_streak function.
  68.      The file MUST be subsequently closed using multic, since
  69.      this function produces a hidden reference to the file.  The function
  70.      multif can be used to return an ordinary file pointer, given the
  71.      returned MF structure.  If the argument is already a stream FILE,
  72.      that file will be used.  The call still produces a hidden copy of
  73.      FILE, so you may set your copy of the FILE variable to [], but do
  74.      not close the file.
  75.  
  76.      The following keywords can be used to allow for variations in the
  77.      variable names or units, and to specify the correspondence between
  78.      the zones in this file, and the zones in the master file:
  79.  
  80.      zonelist=index_list
  81.      -or- zonelist=zonelist_name
  82.        is an index list into the (rt,zt) mesh arrays of the master file.
  83.        If ireg is the region number array (having the same dimensions as
  84.        rt or zt, and with its first row and column all 0), and if FILENAME
  85.        contains opacity data only for zones with region numbers 1 and 2,
  86.        you could open the file using:
  87.           mf= multio(filename, zonelist=where(ireg==1 | ireg==2))
  88.        The zonelist should be nil only if the spatial dimensions of the
  89.        opacity and emissivity in this file exactly match those of rt or
  90.        zt in the master file.
  91.        If zonelist is a string, it replaces the default name for the
  92.        zonelist variable stored in the file (see multi_zonelist).
  93.  
  94.      zoneuse=index_list
  95.        The zonelist specifies how the zones in this file correspond
  96.        with those in the master file.  The zoneuse list allows you
  97.        to specify that only some of the zones actually present in the
  98.        opacity and emissivity arrays of this file are to contribute
  99.        to the total.  This might be necessary to avoid double counting
  100.        in a region covered by more than one file.  Hence zoneuse is
  101.        a list of indices into the spatial dimension(s) of the opacity
  102.        and emissivity arrays in this file.  If nil, all zones in this
  103.        file will contribute.  If present, and if zonelist is supplied
  104.        as an array (rather than out of the file), zonelist should
  105.        have the same length as zoneuse.
  106.        As a special case, if zoneuse is a scalar 0, no opacity or
  107.        emissivity will come from this file; this makes sense only if
  108.        this is the master file.
  109.  
  110.      opac=oname, emiss=ename, srcf=sname
  111.        specify non-default names for the opacity, emissivity, and
  112.        osource function arrays.  The defaults are given by the global
  113.        variables mutli_opac, multi_emiss, and multi_srcf (see help).
  114.        If the emissivity array is present in the file, it is preferred
  115.        to the source function array, which will then be ignored.
  116.  
  117.      oscale=opacity_unit, escale=emissivity_unit
  118.        are optional conversion factors to bring the units of the
  119.        opac and emiss (or srcf) arrays into agreement among the various
  120.        files which are to be used in a single run.  The default value
  121.        is 1.0 (i.e.- all files are expected to have the same units).
  122.  
  123.      gb=gbname, gav=gavname, gexist=gexistname
  124.      -or- gexist=group_existence_map
  125.        specify non-default names for the group boundary, group energy,
  126.        and group existence arrays.  The defaults are given by the global
  127.        variables mutli_gb, multi_gav, and multi_gexist (see help).
  128.        If the group boundary array is present in the file, it is preferred
  129.        to the group energy array, which will then be ignored.  The file
  130.        should specify group boundaries if its opacity and emissivity are
  131.        averaged over finite width bins; group energies if its opacity
  132.        and emissivity are computed at points.  The group existence map,
  133.        if present, allows several disjoint spectral regions to exist in
  134.        a single file.  If the data type of gexist is not "string", it
  135.        should be an array of length one less than gb, if gb is present,
  136.        or gav, otherwise.  By this means you can ignore spectral regions
  137.        which are present in the file.
  138.  
  139.      gscale=photon_energy_unit
  140.        is an optional conversion factor to bring the units of the
  141.        gb (or gav) arrays into agreement among the various files
  142.        which are to be used in a single run.  The default value
  143.        is 1.0 (i.e.- all files are expected to have the same units).
  144.  
  145.      tscale=time_unit
  146.        is an optional conversion factor to bring the units of the
  147.        time into agreement among the various files which are to be used
  148.        in a single run.  The default value is 1.0 (i.e.- all files are
  149.        expected to have the same units).
  150.  
  151.      noextrap=1
  152.        if present and non-zero prevents the opacity and emissivity
  153.        data from this file from being extrapolated as 1/hnu^3 in
  154.        master bins at energies above the highest energy bin in this
  155.        file.
  156.  
  157.      freqfirst=0
  158.        if present and non-zero means the frequency index is first
  159.        for the opacity and emissivity arrays, instead of the
  160.        default of frequency index last.
  161.  
  162.    SEE ALSO: multic, multif, multi_streak, MultiFile
  163.              multi_opac, multi_emiss, multi_srcf, multi_gb, multi_gav,
  164.          multi_zonelist
  165.  */
  166. {
  167.   if (!is_stream(filename)) {
  168.     f= openb(filename);
  169.     if (!is_stream(f)) error, "couldn't openb "+filename;
  170.   } else {
  171.     f= filename;
  172.   }
  173.  
  174.   /* create a symbol and stick a hidden reference to the file in it --
  175.      this is as close as Yorick gets to a pointer to a file variable */
  176.   extern _multi_symbols;
  177.   if (is_void(_multi_symbols)) _multi_symbols= array(string, 8);
  178.   list= where(!_multi_symbols);
  179.   if (!numberof(list)) {
  180.     grow, _multi_symbols, array(string, 8);
  181.     list= where(!_multi_symbols);
  182.   }
  183.   i= list(1);
  184.   _multi_symbols(i)= name= "_m_f_"+pr1(i);
  185.   symbol_set, name, f;
  186.  
  187.   mf= MultiFile(f=name);
  188.   if (!is_void(zonelist) && structof(zonelist)!=string) {
  189.     mf.zones= &zonelist;
  190.     zonelist= [];
  191.   }
  192.   if (!is_void(zoneuse)) mf.zuse= &zoneuse;
  193.   if (!is_void(opac)) mf.oname= opac;
  194.   if (!is_void(emiss)) mf.ename= emiss;
  195.   if (!is_void(srcf)) mf.sname= srcf;
  196.   if (!is_void(escale)) mf.escale= escale;
  197.   if (!is_void(oscale)) mf.oscale= oscale;
  198.   if (!is_void(noextrap)) mf.noextrap= noextrap;
  199.   if (!is_void(freqfirst)) {
  200.     mf.freqfirst= freqfirst;
  201.   } else {
  202.     /* default is zero so q-files will work */
  203.     mf.freqfirst= 0;
  204.   }
  205.   _multi_names, mf, gbname=gb, gavname=gav, gexistname=gexist,
  206.                     gscale=gscale, zonesname=zonelist;
  207.  
  208.   if (!is_void(tscale) && tscale>0.0) mf.tscale= tscale;
  209.   else mf.tscale= 1.0;
  210.  
  211.   return mf;
  212. }
  213.  
  214. _multi_symbols= [];
  215.  
  216. func multic(mf)
  217. /* DOCUMENT multic, mf
  218.          or multic, [mf1, mf2, mf3, ...]
  219.      closes a MultiFile created with multif.
  220.      Presented with an array of multifiles, closes them all.
  221.    SEE ALSO: multio, multif
  222.  */
  223. {
  224.   n= numberof(mf);
  225.   for (i=1 ; i<=n ; i++) {
  226.     name= mf(i).f;
  227.     mf(i).f= "?";    /* prevent future references */
  228.     if (!is_void(_multi_symbols)) {
  229.       list= where(_multi_symbols==name);
  230.       if (numberof(list)) _multi_symbols(list)= string(0);
  231.     }
  232.     /* discard hidden file reference */
  233.     symbol_set, name, [];
  234.     /* free associated storage as well */
  235.     mf.zones= mf.gb= mf.gav= mf.gexist= mf.gx= mf.nu= &[];
  236.   }
  237. }
  238.  
  239. func multif(mf)
  240. /* DOCUMENT multif(mf)
  241.      returns an ordinary file pointer for the MultiFile MF.
  242.      Do not use close to close this pointer; just set it to [] when
  243.      you are done.  Use multic to properly close the MF.
  244.    SEE ALSO: multio, multic
  245.  */
  246. {
  247.   return symbol_def(mf.f);
  248. }
  249.  
  250. func multi_streak(mf, rays, slimits, gb=)
  251. /* DOCUMENT result= multi_streak(mf, rays, slimits, gb=common_bins)
  252.      like the streak function, but allows opacity to be built up from
  253.      "slave files", in addition to the "master file" MF(1).  The MF
  254.      parameter is an array of MultiFiles, each created by multif.
  255.      The master file MF(1) contains the mesh, and the master list of dump
  256.      times.  Only dump times which are present in this master list, and
  257.      in every slave file, will be processed.
  258.      The master file MF(1) need not contain any opacity or emissivity data
  259.      at all; each of the slave files MF(2:0) must contain data for at
  260.      least one zone.
  261.  
  262.      The emissivities and opacities from each file are interpolated onto
  263.      a common group structure.  This common group structure can be
  264.      provided via the GB keyword to multi_streak.  If it is not provided,
  265.      GB is computed by examining the group boundary (or center) arrays
  266.      from the master and every slave file, and building a group structure
  267.      which is at least as fine as every component group structure, at every
  268.      point in the spectrum.
  269.  
  270.      Example:
  271.        File family "prob_p00" contains the mesh and opacities and
  272.        emissivities for all zones.  Family "pp_h00" contains post
  273.        processed opacities and emissivities on a much finer spectral
  274.        mesh, but only for zones in regions 1 and 2 of the original
  275.        problem.  You want to transport the emission from the
  276.        inner regions 1 and 2 through the overlying material:
  277.  
  278.          restore, openb("prob_p00"), ireg;
  279.      master= multif("prob_p00", zoneuse=where(ireg>2));
  280.      slave= multif("pp_h00", zonelist=where(ireg==1|ireg==2));
  281.      rays= ...
  282.      slimits= ...
  283.      drat_start= ...
  284.      drat_stop= ...
  285.      result= multi_streak([master,slave], rays, slimits);
  286.      multic, master;
  287.      multic, slave;
  288.  
  289.    SEE ALSO: multio, multic, multif, MultiFile
  290.              multi_opac, multi_emiss, multi_srcf, multi_gb, multi_gav,
  291.          multi_zonelist
  292.          multi_times   (get list of times used by multi_streak)
  293.          multi_bins    (get bins used by multi_streak)
  294.  */
  295. {
  296.   f= multif(mf(1));       /* master file is first one */
  297.  
  298.   /* initialize local variables which are hidden arguments to
  299.      _multi_... functions */
  300.   _multi_files= mf;
  301.   _multi_gb= gb;
  302.   _multi_nchunks= 0;     /* set in _multi__init */
  303.  
  304.   /* set up hook functions for streak */
  305.   streak_times= multi_times;
  306.   _multi_init= _multi__init;
  307.   drat_integrate= _multi_integrate;
  308.   jt= _multi_jt;
  309.  
  310.   return streak(f, rays, slimits);
  311. }
  312.  
  313. func multi_streak_save(outname, mf, rays, slimits, gb=)
  314. /* DOCUMENT multi_streak_save, outname, mf, rays, slimits, gb=common_bins
  315.          or multi_streak_save, outfile, mf, rays, slimits, gb=common_bins
  316.      like the streak function, but allows opacity to be built up from
  317.      "slave files", in addition to the "master file" MF(1) and 
  318.      saves the streak in a PDB history file.  The MF parameter
  319.      is an array of MultiFiles, each created by multif.
  320.      The master file MF(1) contains the mesh, and the master list of dump
  321.      times.  Only dump times which are present in this master list, and
  322.      in every slave file, will be processed.
  323.      The master file MF(1) need not contain any opacity or emissivity data
  324.      at all; each of the slave files MF(2:0) must contain data for at
  325.      least one zone.
  326.  
  327.      If the first argument is OUTFILE, a file variable instead of a
  328.      file name, then that file is used for output.  You can create
  329.      OUTFILE and add static variables to it with save (but do NOT call
  330.      add_record) which streak_save otherwise wouldn't know about.
  331.  
  332.      The output file has history records at the same times as the
  333.      input file.  Each record contains "time" (a double scalar),
  334.      and the two arrays "transp", the transparency (between 0 and 1),
  335.      and "selfem", the self emission (which has the same units as
  336.      ekap in the file F).  The dimensions of transp and selfem
  337.      are ngroup-by-2-by-nrays (where nrays represents zero or more
  338.      dimensions, copied from the RAYS input array).  The RAYS and
  339.      SLIMITS inputs are placed into the output file as non-record
  340.      variables, and any variables in the drat_static option are
  341.      copied form F to the output file.  The gb and gav variables
  342.      are copied from F into the output file as well.  If the drat_glist
  343.      option is present, that is stored in the output file also.
  344.  
  345.      The emissivities and opacities from each file are interpolated onto
  346.      a common group structure.  This common group structure can be
  347.      provided via the GB keyword to multi_streak.  If it is not provided,
  348.      GB is computed by examining the group boundary (or center) arrays
  349.      from the master and every slave file, and building a group structure
  350.      which is at least as fine as every component group structure, at every
  351.      point in the spectrum.
  352.  
  353.      Example:
  354.        File family "prob_p00" contains the mesh and opacities and
  355.        emissivities for all zones.  Family "pp_h00" contains post
  356.        processed opacities and emissivities on a much finer spectral
  357.        mesh, but only for zones in regions 1 and 2 of the original
  358.        problem.  File "prob_strk" contains the streak history. 
  359.        You want to transport the emission from the
  360.        inner regions 1 and 2 through the overlying material:
  361.  
  362.          restore, openb("prob_p00"), ireg;
  363.      master= multif("prob_p00", zoneuse=where(ireg>2));
  364.      slave= multif("pp_h00", zonelist=where(ireg==1|ireg==2));
  365.      fout= openb("prob_strk");
  366.      save, fout, kmax, lmax;
  367.      rays= ...
  368.      slimits= ...
  369.      drat_start= ...
  370.      drat_stop= ...
  371.      result= multi_streak_save(fout, [master,slave], rays, slimits);
  372.      multic, master;
  373.      multic, slave;
  374.  
  375.    SEE ALSO: multio, multic, multif, MultiFile, multi_streak
  376.              multi_opac, multi_emiss, multi_srcf, multi_gb, multi_gav,
  377.          multi_zonelist
  378.          multi_times   (get list of times used by multi_streak)
  379.          multi_bins    (get bins used by multi_streak)
  380.  */
  381. {
  382.   dummy= use_origins(0);
  383.  
  384.   /* create the output file */
  385.   { local f_save; }    /* used in streak_saver
  386.                           -- must NOT be local variable in streak */
  387.   if (is_stream(outname)) f_save= outname;
  388.   else f_save= createb(outname);
  389.  
  390.   f= multif(mf(1));       /* master file is first one */
  391.  
  392.   /* initialize local variables which are hidden arguments to
  393.      _multi_... functions */
  394.   _multi_files= mf;
  395.   _multi_gb= gb;
  396.   _multi_nchunks= 0;     /* set in _multi__init */
  397.  
  398.   /* set up hook functions for streak */
  399.   streak_times= multi_times;
  400.   _multi_init= _multi__init;
  401.   drat_integrate= _multi_integrate;
  402.   jt= _multi_jt;
  403.  
  404.   /* save rays and slimits parameters to output file */
  405.   if (is_void(slimits)) save, f_save, rays;
  406.   else save, f_save, rays, slimits;
  407.  
  408.   /* copy drat_static from input file to output file */
  409.   { extern drat_static; }
  410.   vars= get_vars(f);
  411.   if (drat_gb && is_present(vars, drat_gb)) list= [drat_gb];
  412.   else list= [];
  413.   if (drat_gav && is_present(vars, drat_gav)) grow, list, [drat_gav];
  414.   grow, list, drat_static;
  415.   n= numberof(list);
  416.   for (i=1 ; i<=n ; i++) {
  417.     name= list(i);
  418.     if (!is_present(vars, name))
  419.       error, "\""+name+"\" specified in drat_static option not present";
  420.     value= get_member(f, name);
  421.     add_variable, f_save, -1, name, structof(value), dimsof(value);
  422.     get_member(f_save, name)= value;
  423.   }
  424.  
  425.   /* save drat_glist option if present */
  426.   { extern drat_glist; }
  427.   if (!is_void(drat_glist)) save, f_save, drat_glist;
  428.  
  429.   /* swap out drat_compress and call streak */
  430.     { extern drat_compress; }     /* option */
  431.   drat_compress= streak_saver;
  432.   streak, f, rays, slimits;
  433. }
  434.  
  435. func multi_bins(mf)
  436. /* DOCUMENT multi_bins(mf)
  437.      The MF parameter is an array of MultiFiles, each created by multif.
  438.      Automatically generates the bin structure which will be used by
  439.      multi_streak (if the GB keyword is not specified).
  440.  */
  441. {
  442.   /* get the group structure from the master file, if any */
  443.   mfi= mf(1);
  444.   if (mfi.gb) gb= *mfi.gb;
  445.   else if (mfi.gav) gb= multi_bav(*mfi.gav);
  446.  
  447.   /* build an appropriate bin boundary array */
  448.   n= numberof(mf);
  449.   for (i=2 ; i<=n ; i++) {
  450.     mfi= mf(i);
  451.     if (mfi.gb) gbf= *mfi.gb;
  452.     else if (mfi.gav) gbf= multi_bav(*mfi.gav);
  453.     else error, "no gb or gav arrays in slave file "+pr1(i);
  454.     if (is_void(gb)) gb= gbf;
  455.     else gb= _multi_bins(0, gb, gbf);
  456.   }
  457.  
  458.   return gb;
  459. }
  460.  
  461. func _multi_jt(f, time)
  462. {
  463.   _jt, f, time;
  464.   n= numberof(_multi_files);
  465.   for (i=2 ; i<=n ; i++) {
  466.     mfi= _multi_files(i);
  467.     _jt, multif(mfi), time/mfi.tscale;
  468.   }
  469. }
  470.  
  471. /* streak_times is "swapped out" in multi_streak */
  472. _orig_streak_times= streak_times;
  473.  
  474. func multi_times(f)
  475. /* DOCUMENT times= multi_times(mf)
  476.      returns the list of times which will be used by multi_streak.  This
  477.      is the subset of streak_times(mf(1)) which occur in all of the slave
  478.      files.  The drat_start and drat_stop times work as usual.
  479.  */
  480. {
  481.   if (!is_stream(f)) {
  482.     mf= f;
  483.     f= multif(mf(1));
  484.   } else {
  485.     mf= _multi_files;  /* if called as streak_times from streak function,
  486.               _multi_files is local to multi_streak */
  487.   }
  488.   if (!is_void(mf) &&
  489.       abs(mf(1).tscale-1.0)>1.e-6) error, "tscale illegal in master file";
  490.   times= _orig_streak_times(f);
  491.  
  492.   n= numberof(mf);
  493.   for (i=2 ; i<=n ; i++) {
  494.     mfi= mf(i);
  495.     times= _multi_times(times, mfi.tscale*get_times(multif(mfi)));
  496.   }
  497.  
  498.   /* give the multi package a chance to initialize just before the main
  499.      loop over time is entered in the streak function */
  500.   if (!is_void(_multi_init)) _multi_init;
  501.  
  502.   return times;
  503. }
  504.  
  505. local multi_memory;
  506. /* DOCUMENT multi_memory
  507.      amount of memory used to determine size of spectral chunks.
  508.      Default is 2000000, which keeps the memory required per chunk
  509.      to under a few megabytes.
  510.  */
  511. multi_memory= 2000000;
  512.  
  513. func _multi__init
  514. /* xxDOCUMENT multi_init
  515.      is called immediately before the streak function enters its loop
  516.      over time.  It is responsible for computing all of the photon group
  517.      interpolation weights for the master and slave files.
  518.  */
  519. {
  520.   extern _multi_files, _multi_gb;  /* local to multi_streak */
  521.  
  522.   /* build an appropriate bin boundary array if necessary */
  523.   if (is_void(_multi_gb)) _multi_gb= multi_bins(_multi_files);
  524.  
  525.   /* compute a reasonable number of chunks, trying to limit memory
  526.      usage to a few megabytes */
  527.   extern _multi_nchunks;    /* local to multi_streak */
  528.   kmx= dimsof(get_member(multif(_multi_files(1)), drat_rt));
  529.   lmx= kmx(3);
  530.   kmx= kmx(2);
  531.   ngroup= numberof(_multi_gb)-1;
  532.   _multi_nchunks= min(max(multi_memory/(8*8*kmx*lmx), 1), ngroup);
  533.   if (ngroup%_multi_nchunks) _multi_nchunks= ngroup/_multi_nchunks + 1;
  534.   else _multi_nchunks= ngroup/_multi_nchunks;
  535.  
  536.   /* compute the various indices and other data required for the
  537.      interpolation and splitting into chunks */
  538.   n= numberof(_multi_files);
  539.   for (i=1 ; i<=n ; i++)
  540.     _multi_chunk, i, _multi_gb, _multi_nchunks;
  541. }
  542.  
  543. func _multi_integrate(f, mesh, time, irays, slimits)
  544. /* DOCUMENT atten_emit= _multi_integrate(f, mesh, time, irays, slimits)
  545.      is the default drat_integrate routine.
  546.      On entry, file F is positioned at TIME, from which MESH has already
  547.      been read.  IRAYS and SLIMITS are the rays coordinates (in internal
  548.      format) and integration limits.
  549.      The result should be ngroup-by-2-by-raydims, where the second index
  550.      is 1 for the attenuation factor, 2 for the self-emission (specific
  551.      intensity due to emission along the ray).
  552.    OPTIONS: drat_linear, drat_ocompute, drat_oadjust,
  553.             drat_emult, drat_amult, drat_omult, drat_nomilne,
  554.         drat_ekap, drat_akap, drat_glist
  555.    SEE ALSO: streak, multi_streak
  556.  */
  557. {
  558.   /* build arrays which will hold the opacity and emissivity chunks */
  559.   ngroup= numberof(_multi_gb)-1;
  560.   size= ngroup/_multi_nchunks;
  561.   nlong= ngroup%_multi_nchunks;
  562.   if (nlong) size++;
  563.   /* local here, external to _multi_accum */
  564.   opac= emiss= array(0.0, dimsof(get_member(f,drat_rt)), size);
  565.  
  566.   /* select the appropriate integrator */
  567.   integrator= drat_linear? integ_linear : integ_flat;
  568.  
  569.   /* loop over spectral chunks */
  570.   nfiles= numberof(_multi_files);
  571.   n2= 0;
  572.   for (i=1 ; i<=_multi_nchunks ; i++) {
  573.     n1= n2+1;
  574.     n2+= size;
  575.  
  576.     /* accumulate opacities and emissivities from all files */
  577.     for (j=1 ; j<=nfiles ; j++)
  578.       _multi_accum, _multi_files(j), _multi_gb(n1:n2+1), i;
  579.  
  580.     /* do transport integral for these rays cutting through current mesh */
  581.     emiss/= opac+1.e-99;  /* do it here to reduce number of temporaries */
  582.  
  583.     /* apply absorption and emission multipliers */
  584.     { extern drat_amult, drat_emult, drat_omult; }   /* options */
  585.     if (!is_void(drat_emult)) emiss*= drat_emult;
  586.     if (!is_void(drat_amult)) {
  587.       opac*= drat_amult+1.e-20;
  588.       emiss/= drat_amult+1.e-20;
  589.     }
  590.     if (!is_void(drat_omult)) opac*= drat_omult;
  591.  
  592.     /* point center the source function (in place) if requested */
  593.     { extern drat_linear, drat_nomilne; }   /* options */
  594.     if (drat_linear) {
  595.       if (structof(emiss)!=double) emiss= double(emiss);
  596.       pcen_source, opac, emiss, mesh, drat_nomilne;
  597.     }
  598.  
  599.     chunk= integrator(opac, emiss, irays, mesh, slimits);
  600.  
  601.     if (i==1) {
  602.       /* result is same as chunk except in spectral dimension,
  603.      which is length ngroup instead of chunk size */
  604.       dims= dimsof(chunk);
  605.       dims(2)= ngroup;
  606.       instant= array(0.0, dims);
  607.     }
  608.     instant(n1:n2,..)= chunk;
  609.     chunk= [];  /* free memory ASAP */
  610.  
  611.     if (i==nlong) {
  612.       /* ngroup%_multi_nchunks chunks are length ngroup/_multi_nchunks+1,
  613.      the rest are length ngroup/_multi_nchunks
  614.      Adjust size for the next pass -- note that nlong<_multi_nchunks
  615.      by construction, so this can never happen on the last pass; it
  616.      never happens at all if nlong==0.  */
  617.       size--;
  618.       dims= dimsof(opac);
  619.       dims(0)= size;
  620.       opac= emiss= [];  /* destroy before recreating */
  621.       opac= emiss= array(0.0, dims);
  622.     } else {
  623.       opac(..)= emiss(..)= 0.0;  /* initialize for next pass */
  624.     }
  625.   }
  626.  
  627.   return instant;
  628. }
  629.  
  630. /* ------------------------------------------------------------------------ */
  631.  
  632. struct MultiFile {
  633.   string f;         /* name of the file variable for this file
  634.                -- no such thing as address of a file variable */
  635.  
  636.   string oname;     /* name of the opacity variable in f
  637.                -- default is "opac" if present in the file */
  638.   string ename;     /* name of the emissivity variable in f, or 0 if
  639.                source function used instead
  640.                -- default is "emiss" if present in the file */
  641.   string sname;     /* name of the source function variable in f,
  642.                ignored if ename!=0
  643.                -- default is "srcf" if present in the file */
  644.  
  645.   double oscale;    /* scale factor for correcting opacity units */
  646.   double escale;    /* scale factor for correcting emissivity units
  647.                -- will be used to correct srcf*opac if srcf used */
  648.  
  649.   pointer zuse;     /* pointer to list of zone indices to use in this file
  650.                -- If &[], all zones in this file are used.
  651.                -- As a special case, if &array(0), use no zones.  */
  652.  
  653.   pointer zones;    /* list of master file zone indices for the zones which
  654.                are present in this post-processing file
  655.                -- The length of this list must be the same as the
  656.                product of the non-spectral dimensions of the opacity
  657.                and emissivity arrays, or the same length as *zuse,
  658.                if that array is present.
  659.                -- zones==0 means that all zones in the master file
  660.                (including zeroes in the first row and column) are
  661.                present in this file.  */
  662.  
  663.   /* This standard interface supports two spectral models:
  664.      (1) Zone-centered model: Opacities and emissivities are defined as
  665.          integrals over photon energy bins, so that the group structure
  666.      is specified by a bin boundary energy list gb.
  667.      (2) Point-centered model: Opacities and emissivities are defined at
  668.          particular discrete photon energies, so that the group structure
  669.      is specified by a photon energy list gav.
  670.      In case (1), gav==0, and in case (2), gb==0.
  671.      In either case, gb or gav must be a strictly increasing list of
  672.      photon energies.  However, a single file may contain data in several
  673.      disjoint spectral regions.  This information, together with spectral
  674.      chunking information, is stored in the index array, nu.
  675.    */
  676.   pointer gb;       /* bin boundary array list for this file */
  677.   pointer gav;      /* photon energy array list for this file */
  678.   pointer gexist;   /* group existence map (if gb!=0) or connection map
  679.                (if gav!=0), or 0 if all groups exist or all
  680.                photon energies are connected */
  681.  
  682.   pointer gx;
  683.   /* (*gx) is a pointer to a n_spectral_regions -by- 2 array
  684.      of extreme values of each spectral region, used to limit the
  685.      interpolation boundaries.  (*gx)(,1) is the lower boundary,
  686.      while (*gx)(,2) is the upper boundary.  */
  687.  
  688.   pointer nu;
  689.   /* (*nu) is 6 -by- n_spectral_regions -by- n_chunks index array:
  690.      (*nu)(, spectral-region, chunk)
  691.      can be written [i1,i2,i3,i4,i5,i6] where:
  692.  
  693.      gb(i1:i2+1) or gav(i1:i2) are the bins required to compute this
  694.           chunk in this spectral region.
  695.       i1==0 if, and only if, this file make no contribution at all
  696.       to this chunk in this spectral region.
  697.      multi_gb(i3:i4) are the master bins affected by this chunk
  698.      If i6!=0, opacity(i4+1:i5) will be extrapolated as 1/nu^3 from the
  699.      value at gb(i6:i6+1) or gav(i6).  This will be done even if i1==0.  */
  700.  
  701.   double tscale;    /* scale factor for correcting time units */
  702.  
  703.   int noextrap;  /* 1/nu^3 extrapolation will be done unless non-zero */
  704.  
  705.   int freqfirst;  /* frequency index is last unless non-zero */
  706. }
  707.  
  708. func _multi_chunk(i, gb, nchunks)
  709. /* xxDOCUMENT _multi_chunk, i, gb, nchunks
  710.      computes the nu and gi members of the MultiFile struct _multi_files(I),
  711.      so that the master bin boundary array GB can be computed in NCHUNKS
  712.      chunks.
  713.  */
  714. {
  715.   mf= _multi_files(i);
  716.   /* get group structure out of file */
  717.   gbf= *mf.gb;
  718.   gavf= *mf.gav;
  719.   gexist= *mf.gexist;
  720.   if (is_void(gbf) && is_void(gavf)) return;
  721.  
  722.   /* compute chunk boundaries */
  723.   nb= numberof(gb);
  724.   ngroup= nb-1;
  725.   if (nchunks>ngroup) nchunks= ngroup;
  726.   chunk= array(ngroup/nchunks, nchunks);
  727.   n= ngroup%nchunks;
  728.   if (n) chunk(1:n)+= 1;
  729.   chunk= chunk(cum);
  730.   cbot= chunk(1:-1)+1;
  731.   ctop= chunk(2:0);
  732.  
  733.   /* get j1= indices of beginnings of spectral regions,
  734.          j2= indices of ends of spectral regions
  735.      in file bins */
  736.   j1= _multi_spectreg(gexist, gbf, gavf);
  737.   j2= j1(,2);
  738.   j1= j1(,1);
  739.  
  740.   /* get master bin numbers containing j1 and j2
  741.      -- after this, bot and top are bin number containing the
  742.         bottom and top of each of the spectral regions
  743.      -- 1 and ngroup+2 are still possible if the region partially
  744.         hangs over the master bin structure */
  745.   if (!is_void(gbf)) {
  746.     gf= gbf;
  747.     nf= max(j2)-1;   /* top group index in file */
  748.   } else {
  749.     gf= gavf;
  750.     nf= max(j2);
  751.   }
  752.   gbot= gf(j1);
  753.   gtop= gf(j2+(!is_void(gbf)));
  754.   bot= digitize(gbot, gb)-1;
  755.   top= digitize(-gtop, -gb)-1;  /* if one of the gtop is exactly equal to
  756.                    one of the gb, want the smaller bin
  757.                    for top (see digitize help) */
  758.  
  759.   /* get the master group indices of the minimum i3 and maximum i4 group
  760.      which can be affected by each spectral region for each chunk
  761.      -- note that i3>=1 and i4<=ngroup are guaranteed after this, so
  762.         gb(i3) and gb(i4+1) below will never blow up */
  763.   i3= max(bot, cbot(-,));
  764.   i4= min(top, ctop(-,));
  765.  
  766.   /* get minimum i1 and maximum i2 bin indices in file
  767.      which must be extracted for each spectral region and each chunk
  768.      -- note that i3>i4 in empty spectral regions, which will be
  769.         removed later */
  770.   i1= digitize(gb(i3), gf)-1;
  771.   i2= digitize(-gb(i4+1), -gf);  /* see digitize comment above */
  772.   if (!is_void(gbf)) i2--;  /* top group one less than top bin boundary */
  773.   /* can't retrieve anything beyond min(gf) and max(gf) */
  774.   i1= max(1, i1);
  775.   i2= min(nf, i2);
  776.  
  777.   /* zero empty spectral region/chunks -- they haven't been computed
  778.      correctly anyway */
  779.   list= where(i3>i4);
  780.   if (numberof(list)) i1(list)= i2(list)= i3(list)= i4(list)= 0;
  781.  
  782.   /* find spectral regions where extrapolation is necessary */
  783.   cbot-= 1;
  784.   cbot= cbot(-:1:numberof(j2),);
  785.   i5= i6= 0*i1;
  786.   list= where(top<ctop(-,));  /* see definition of i4 above */
  787.   if (!mf.noextrap && numberof(list)) {
  788.     i6(list)= j2(,-:1:numberof(ctop))(list);
  789.     i5(list)= ctop(-:1:numberof(j2),)(list);
  790.     i4e= i4(list);
  791.     l= where(i4e==0);
  792.     if (numberof(l)) {
  793.       /* fix any chunks which require extrapolation, but which are
  794.      completely above the spectral region being extrapolated */
  795.       i4e(l)= cbot(list)(l);
  796.       i4(list)= i4e;
  797.     }
  798.   }
  799.  
  800.   /* adjust i3, i4, and i5 to apply to the current chunk, rather
  801.      than to the entire gb array */
  802.   list= where(i3);
  803.   if (numberof(list)) i3(list)-= cbot(list);
  804.   list= where(i4);
  805.   if (numberof(list)) i4(list)-= cbot(list);
  806.   list= where(i5);
  807.   if (numberof(list)) i5(list)-= cbot(list);
  808.  
  809.   /* install index lists into mf */
  810.   _multi_files(i).nu= &transpose([i1,i2,i3,i4,i5,i6],2);
  811.  
  812.   /* generate the interpolation bin limits for each spectral region
  813.      and install into mf */
  814.   _multi_files(i).gx= &[gbot, gtop];
  815. }
  816.  
  817. func _multi_spectreg(gexist, gbf, gavf)
  818. /* xxDOCUMENT _multi_spectreg(gexist, gbf, gavf)
  819.      crack GBF (if non-nil) or GAVF (if GBF nil) into spectral regions
  820.      according to GEXIST.  Returns 2-by-n_spectral_regions array of
  821.      minimum and maximum group indices for each spectral region.
  822.      In the case of GBF, GEXIST is a bin existence map, dimension one
  823.      less than GBF, non-zero in bins which exist.
  824.      In the case of GAVF, GEXIST is a photon connectivity array, dimension
  825.      one less than GAVF, zero between consecutive elements of GAVF which
  826.      are not connected.  Single, isolated points are removed entirely.
  827.      In either case, GAVF or GBF must be a strictly increasing list.
  828.  */
  829. {
  830.   n= numberof(gbf);
  831.   if (is_void(gexist)) {
  832.     j1= [1];
  833.     j2= [(n? n-1 : numberof(gavf))];
  834.   } else {
  835.     gexist= grow([0], (gexist!=0), [0]);
  836.     gexist= gexist(dif);
  837.     j1= where(gexist>0);  /* off to on transitions */
  838.     j2= where(gexist<0);  /* on to off transitions */
  839.     if (numberof(j2)) {
  840.       if (n) {
  841.     j2--;   /* bin index, not boundary index */
  842.       } else if (anyof(j2==j1+1)) {
  843.     list= where(j2>j1+1);
  844.     j1= j1(list);
  845.     j2= j2(list);
  846.       }
  847.     }
  848.     if (numberof(j2)<1) error, "group existence map for file is void";
  849.   }
  850.   return [j1,j2];
  851. }
  852.  
  853. func _multi_accum(mf, gb, chunk)
  854. /* xxDOCUMENT _multi_accum, mf, gb, chunk
  855.      increments externals opac, emiss from MultiFile MF, chunk number
  856.      CHUNK.  GB is the master bin boundary array.
  857.  */
  858. {
  859.   /* unpack the mf MultiFile struct */
  860.   nu= mf.nu;
  861.   if (!nu) return;         /* detect no-op quickly */
  862.   nu= (*nu)(,,chunk);
  863.   if (noneof(nu)) return;  /* detect no-op quickly */
  864.   f= multif(mf);
  865.   oname= mf.oname;
  866.   ename= mf.ename;
  867.   sname= mf.sname;
  868.   oscale= mf.oscale;
  869.   escale= mf.escale;
  870.   freqfirst= mf.freqfirst;
  871.   zuse= *mf.zuse;   /* if scalar 0, nu==&[] above, and won't get here */
  872.   zones= *mf.zones;
  873.   gbf= *mf.gb;
  874.   gavf= *mf.gav;
  875.   gx= (*mf.gx);
  876.   gbot= gx(,1);
  877.   gtop= gx(,2);
  878.  
  879.   n= dimsof(nu)(3);
  880.   for (i=1 ; i<=n ; i++) {
  881.     /* loop over disjoint spectral regions in this file
  882.        -- this is a little inefficient, as presumably at most two
  883.           adjacent ones will have non-zero i1 */
  884.     i1= nu(1,i);
  885.     i2= nu(2,i);
  886.     i3= nu(3,i);
  887.     i4= nu(4,i);
  888.     i5= nu(5,i);
  889.     i6= nu(6,i);
  890.  
  891.     if (i1) {
  892.       /* compute integral of opacity and emissivity over this chunk,
  893.          interpolated onto the master bin structure */
  894.       gbsub= gb(i3:i4+1);
  895.       gbm= min(max(gbsub,gbot(i)),gtop(i));
  896.       /* NOTE: oraw is two-dimensional */
  897.       if(freqfirst) {
  898.     oraw= transpose(get_member(f,oname)(i1:i2,*));
  899.       } else {
  900.     oraw= get_member(f,oname)(*,i1:i2);
  901.       }
  902.       if (!is_void(gbf)) {
  903.     /* zone-centered spectral model */
  904.     g12= gbf(i1:i2+1);
  905.     dg12= g12(-,dif);
  906.     if (is_void(gavf)) o= interp((oraw*dg12)(,cum), g12, gbm, 2);
  907.     else o= integ(oraw, gavf(i1:i2), gbm, 2);
  908.     if (ename) {
  909.       oraw= [];
  910.       if(freqfirst) {
  911.         e= transpose(get_member(f,ename)(i1:i2,*));
  912.         e= interp((e*dg12)(,cum), g12, gbm, 2);
  913.       } else {
  914.         e= interp((get_member(f,ename)(*,i1:i2)*dg12)(,cum), g12, gbm, 2);
  915.       }
  916.     } else {
  917.       if(freqfirst) {
  918.         e= transpose(oraw*get_member(f,sname)(i1:i2,*));
  919.         e= interp((e*dg12)(,cum), g12, gbm, 2);
  920.       } else {
  921.         e= interp((oraw*get_member(f,sname)(*,i1:i2)*dg12)(,cum),
  922.               g12, gbm, 2);
  923.       }
  924.       oraw= [];
  925.     }
  926.       } else {
  927.     /* point-centered spectral model */
  928.     g12= gavf(i1:i2);
  929.     dg12= g12(-,dif);
  930.     o= integ(oraw, g12, gbm, 2);
  931.     if (ename) {
  932.       oraw= [];
  933.       if(freqfirst) {
  934.         e= transpose(get_member(f,ename)(i1:i2,*));
  935.         e= integ(e, g12, gbm, 2);
  936.       } else {
  937.         e= integ(get_member(f,ename)(*,i1:i2), g12, gbm, 2);
  938.       }
  939.     } else {
  940.       if(freqfirst) {
  941.         e= transpose(oraw*get_member(f,sname)(i1:i2,*));
  942.         e= integ(e, g12, gbm, 2);
  943.       } else {
  944.         e= integ(oraw*get_member(f,sname)(*,i1:i2), g12, gbm, 2);
  945.       }
  946.       oraw= [];
  947.     }
  948.       }
  949.  
  950.       /* differentiate and scale */
  951.       dgb= gbsub(-,dif);
  952.       o= o(,dif)/(dgb/oscale);
  953.       e= e(,dif)/(dgb/escale);
  954.  
  955.       /* accumulate into opac and emiss externals */
  956.       if (!is_void(zuse)) {
  957.     /* only a subset of the zones present in this file contribute */
  958.     o= o(zuse,);
  959.     e= e(zuse,);
  960.       }
  961.       if (is_void(zones)) {
  962.     /* all zones represented in this file */
  963.     opac(*,i3:i4)+= o;
  964.     emiss(*,i3:i4)+= e;
  965.       } else {
  966.     /* subset of zones in this file */
  967.     opac(zones,1,i3:i4)+= o;
  968.     emiss(zones,1,i3:i4)+= e;
  969.       }
  970.     }
  971.  
  972.     if (i6) {
  973.       /* must extrapolate opacity as 1/hnu^3 and emissivity as
  974.          exp(-hnu/kT) */
  975.       /* first, get gb3, proportional to the integral of 1/hnu^3 across
  976.      the master bins gb (assumed external) */
  977.       gb3= gb(i4+1:i5+1);
  978.       gb3= gb3(zcen)/(gb3(1:-1)*gb3(2:0))^2;
  979.       if (!is_void(gbf)) gf= avg(gbf(i6:i6+1));
  980.       else gf= gavf(i6);
  981.       gb3*= gf^3;
  982.  
  983.       /* next, get the opacity and emissivity in bin i6 of this file */
  984.       if(freqfirst) {
  985.     o= transpose(get_member(f,oname)(i6,*));
  986.       } else {
  987.     o= get_member(f,oname)(*,i6);
  988.       }
  989.       if (ename) {
  990.     if(freqfirst) {
  991.       e= transpose(get_member(f,ename)(i6,*));
  992.     } else {
  993.       e= get_member(f,ename)(*,i6);
  994.     }
  995.       } else {
  996.     if(freqfirst) {
  997.       e= transpose(o*get_member(f,sname)(i6,*));
  998.     } else {
  999.       e= o*get_member(f,sname)(*,i6);
  1000.     }
  1001.       }
  1002.  
  1003.       if (!is_void(zuse)) {
  1004.     /* only a subset of the zones present in this file contribute */
  1005.     o= o(zuse);
  1006.     e= e(zuse);
  1007.       }
  1008.       if (is_void(zones)) {
  1009.     /* all zones represented in this file */
  1010.     opac(*,i4+1:i5)+= o*(gb3(-,)*oscale);
  1011.     emiss(*,i4+1:i5)+= e*(gb3(-,)*escale);
  1012.       } else {
  1013.     /* subset of zones in this file */
  1014.     opac(zones,1,i4+1:i5)+= o*(gb3(-,)*oscale);
  1015.     emiss(zones,1,i4+1:i5)+= e*(gb3(-,)*escale);
  1016.       }
  1017.     }
  1018.  
  1019.     /* free temporary storage before next pass */
  1020.     o= e= [];
  1021.  
  1022.   }  /* end of loop over spectral regions */
  1023. }
  1024.  
  1025. local multi_opac, multi_emiss, multi_srcf;
  1026. /* DOCUMENT multi_opac, multi_emiss, multi_srcf
  1027.      are the default names of the opacity, emissivity, and source function
  1028.      arrays in post-processing files.  By default, they are "opac",
  1029.      "emiss", and "srcf", respectively.  If none of these are present,
  1030.      drat_akap and drat_ekap will also be tried for the opacity and
  1031.      source function, respectively.  If emissivity is present, source
  1032.      function will be ignored.
  1033.      The units of opacity are inverse length, of emissivity power per
  1034.      photon energy per sterradian per volume, and source function
  1035.      power per photon energy per sterradian per area.
  1036.  */
  1037.  
  1038. local multi_gb, multi_gav, multi_gexist;
  1039. /* DOCUMENT multi_gb, multi_gav, multi_gexist
  1040.      are the default names of the group boundary, group center, and group
  1041.      existence arrays in post-processing files.  By default, they are
  1042.      "gb", "gav", and "gexist".  If neither is present, drat_gb and
  1043.      drat_gav will also be tried.
  1044.      If present, gb is the photon bin boundary array, and gexist (if
  1045.      present) has one fewer element and is non-zero in bins which exist.
  1046.      Otherwise, gav is the photon energy array, and gexist (if present)
  1047.      has one fewer element and is zero between elements of gav which are
  1048.      not connected.  Isolated points in gav are removed entirely.
  1049.      Either gb or gav must be strictly increasing, and has units of
  1050.      photon energy.
  1051.  */
  1052.  
  1053. local multi_zonelist;
  1054. /* DOCUMENT multi_zonelist
  1055.      is the default name of the variable which is a list of 1-origin
  1056.      zone indices in the mesh of the master file.
  1057.  */
  1058.  
  1059. func _multi_names(mf, gbname=, gavname=, gexistname=, gscale=, zonesname=)
  1060. /* xxDOCUMENT _multi_names, mf, gbname=..., gavname=...,
  1061.                             gexistname=..., gscale=..., zonesname=...
  1062.      find all relevant variable names in the file specified by MF.f.
  1063.      Reads in the gb (or gav) and gexist arrays, setting MF.gb, MF.gav,
  1064.      and MF.gexist appropriately, multiplying the values read from the
  1065.      file by GSCALE if supplied.
  1066.      Reads in MF.zones if not already set and if zonelist is present in
  1067.      the file.
  1068.      If MF.oscale or MF.escale is 0.0, it is set to 1.0; if less than
  1069.      zero, it is set to 0.0.
  1070.      If the routine fails, mf.oname will be string(0) on output.
  1071.  */
  1072. {
  1073.   f= multif(mf);
  1074.   vars= get_vars(f);
  1075.  
  1076.   zuse= *mf.zuse;
  1077.   contrib= (is_void(zuse) || zuse(1));
  1078.  
  1079.   if (contrib) {
  1080.  
  1081.     if (!mf.zones) {
  1082.       if (is_void(zonesname)) zonesname= multi_zonelist;
  1083.       if (is_present(vars, zonesname)) zones= get_member(f, zonesname);
  1084.       if (!is_void(zones) && !is_void(zuse)) zones= zones(zuse);
  1085.       mf.zones= &zones;
  1086.     }
  1087.  
  1088.     if (mf.oname && !is_present(vars, mf.oname)) mf.oname= string(0);
  1089.     if (!mf.oname) {
  1090.       if (is_present(vars, multi_opac)) mf.oname= multi_opac;
  1091.       else if (is_present(vars, drat_akap)) mf.oname= drat_akap;
  1092.     }
  1093.  
  1094.     if (mf.ename && !is_present(vars, mf.ename)) mf.ename= string(0);
  1095.     if (mf.sname && !is_present(vars, mf.sname)) mf.sname= string(0);
  1096.     if (!mf.ename && !mf.sname) {
  1097.       if (is_present(vars, multi_emiss)) mf.ename= multi_emiss;
  1098.       else if (is_present(vars, multi_srcf)) mf.sname= multi_srcf;
  1099.       else if (is_present(vars, drat_ekap)) mf.sname= drat_ekap;
  1100.       else mf.oname= string(0);
  1101.     }
  1102.  
  1103.     if (!mf.gb && !mf.gav && mf.oname) {
  1104.       if (is_void(gbname) && is_void(gavname)) {
  1105.     if (is_present(vars, multi_gb)) gbname= multi_gb;
  1106.     else if (is_present(vars, drat_gb)) gbname= drat_gb;
  1107.     if (is_present(vars, multi_gav)) gavname= multi_gav;
  1108.     else if (is_present(vars, drat_gav)) gavname= drat_gav;
  1109.       } else {
  1110.     if (!is_void(gbname) && !is_present(vars, gbname))
  1111.       error, "gbname= "+gbname+" is not present in file";
  1112.     if (!is_void(gavname) && !is_present(vars, gavname))
  1113.       error, "gavname= "+gavname+" is not present in file";
  1114.       }
  1115.       if (is_void(gexistname)) {
  1116.     if (is_present(vars, multi_gexist)) gexistname= multi_gexist;
  1117.       } else {
  1118.     if (!is_void(gexistname) && !is_present(vars, gexistname))
  1119.       error, "gexistname= "+gexistname+" is not present in file";
  1120.       }
  1121.       if (!gscale) gscale= 1.0;
  1122.  
  1123.       if (!is_void(gexistname)) mf.gexist= &get_member(f, gexistname);
  1124.       if (!is_void(gbname)) mf.gb= &(gscale*get_member(f, gbname));
  1125.       if (!is_void(gavname)) mf.gav= &(gscale*get_member(f, gavname));
  1126.       if (!mf.gb && !mf.gav) mf.oname= mf.ename= string(0);
  1127.     }
  1128.  
  1129.     if (mf.oscale==0.0) mf.oscale= 1.0;
  1130.     else if (mf.oscale<0.0) mf.oscale= 0.0;
  1131.     if (mf.escale==0.0) mf.escale= 1.0;
  1132.     else if (mf.escale<0.0) mf.escale= 0.0;
  1133.  
  1134.   } else {
  1135.     mf.oname= string(0);
  1136.     mf.gb= mf.gav= &[];
  1137.   }
  1138. }
  1139.  
  1140. /* ------------------------------------------------------------------------ */
  1141. /* procedures to assist in combining different sets of photon bins, or
  1142.    otherwise generating photon group structure */
  1143.  
  1144. func multi_bav(gav)
  1145. /* DOCUMENT multi_bav(gav)
  1146.      returns bin boundaries for the bin centers gav.
  1147.      The bin boundaries are taken at the geometric means between
  1148.      consecutive gav(i), with the endpoints extended slightly beyond
  1149.      the endpoints of gav.
  1150.  */
  1151. {
  1152.   gb= log(gav + 1.e-99)(pcen);
  1153.   gb(1)-= gb(2)-gb(1);
  1154.   gb(0)+= gb(0)-gb(-1);
  1155.   return exp(gb);
  1156. }
  1157.  
  1158. func _multi_bins(nfinal, ..)
  1159. /* DOCUMENT gb= _multi_bins(nfinal, gb1, gb2, ...)
  1160.      returns NFINAL+1 boundaries of NFINAL bins constructed by combining
  1161.      the input bin structures GB1, GB2, etc.
  1162.      Use NFINAL=0 to get at least the resolution in the finest GBi in
  1163.      every region of the spectrum.
  1164.  
  1165.      This is done by constructing a total bin density function
  1166.      (#bins/energy width), as the maximum of the bin density of each
  1167.      component.  This total bin density function is integrated, and
  1168.      the integral is divided into NFINAL equal parts; the points in
  1169.      energy at which this division must be made are the returned bin
  1170.      boundaries.
  1171.  
  1172.      In the returned bin structure, the density of bins is everywhere
  1173.      proportional to the densest bins in any of the GBi.
  1174.  */
  1175. {
  1176.   boundary= density= [];
  1177.   while (more_args()) {
  1178.     /* get next input boundary array */
  1179.     gb= next_arg();
  1180.     if (is_void(gb)) continue;
  1181.     gd= 1./gb(dif);  /* bin density is zone centered -- constant
  1182.             between the bin boundaries */
  1183.     nb= numberof(boundary);
  1184.  
  1185.     if (!nb) {
  1186.       /* first pass */
  1187.       boundary= gb;
  1188.       density= gd;
  1189.  
  1190.     } else {
  1191.       /* subsequent passes */
  1192.       /* combine lists of bin boundaries, sort into increasing order,
  1193.      and eliminate any duplicates */
  1194.       combine= grow(boundary, gb);
  1195.       combine= combine(sort(combine));
  1196.       combine= multi_no_dups(combine);
  1197.  
  1198.       /* get lists of indices of the center of each of the new bins in
  1199.      the old boundary and gb arrays */
  1200.       comcen= combine(zcen);
  1201.       list1= digitize(comcen, boundary);
  1202.       list2= digitize(comcen, gb);
  1203.  
  1204.       /* add guard zones for points beyond boundary or gb separately */
  1205.       density= grow([0.], density, [0.]);
  1206.       gd= grow([0.], gd, [0.]);
  1207.  
  1208.       /* the new density is the maximum of the two component densities,
  1209.      and the new boundary is the combination of the two component
  1210.      boundaries */
  1211.       density= max(density(list1), gd(list2));
  1212.       boundary= combine;
  1213.     }
  1214.   }
  1215.   if (is_void(boundary)) return [];
  1216.  
  1217.   /* now have a histogram density(boundary) of the maximum bin density
  1218.      between every energy mentioned in any of the gb -- the integral of
  1219.      this histogram is the piecewise linear function total(boundary): */
  1220.   total= (density*boundary(dif))(cum);
  1221.  
  1222.   /* note that the last point, total(0) is the "natural" number of bins
  1223.      required to get at least the spectral resolution in any of the
  1224.      input gb */
  1225.   if (nfinal<1) nfinal= long(total(0)+0.99);
  1226.  
  1227.   /* should this be enhanced to allow the caller to specify endpoints
  1228.      other than boundary(1) and boundary(0)? */
  1229.   return interp(boundary, total/total(0), span(0.,1.,nfinal+1));
  1230. }
  1231.  
  1232. func multi_line(nbins, hnu0, dhnu, dhnu_min)
  1233. /* DOCUMENT gb= multi_line(nbins, hnu0, dhnu, dhnu_min)
  1234.      returns 2*NBINS+1 bin boundary energies for 2*NBINS bins
  1235.      cenetered around a spectral line at HNU0 of width DHNU.  The
  1236.      result begins at HNU0-DHNU and ends at HNU0+DHNU.  The finest
  1237.      two bins (nearest HNU0) has width DHNU_MIN, and the remaining
  1238.      bins have equal ratio widths as you move away from HNU0.
  1239.  */
  1240. {
  1241.   dhnu= double(dhnu);
  1242.   r= 0.;
  1243.   if (dhnu_min && dhnu_min>0. && dhnu_min<dhnu) {
  1244.     require, "series.i";
  1245.     r= gseries_r(dhnu/dhnu_min, nbins);
  1246.   }
  1247.   if (r>1.) widths= dhnu_min*exp(log(r)*indgen(0:nbins-1));
  1248.   else widths= dhnu/nbins;
  1249.   widths= grow(widths(0:1:-1), widths);
  1250.   return hnu0-dhnu + widths(cum);
  1251. }
  1252.  
  1253. /* ------------------------------------------------------------------------ */
  1254. /* random worker routines */
  1255.  
  1256. func multi_no_dups(x)
  1257. /* DOCUMENT xnd= multi_no_dups(x)
  1258.      returns its input vector X with any duplicate values removed.
  1259.      X must be non-decreasing and of length at least two.
  1260.  */
  1261. {
  1262.   return x(grow([1], where(x(dif)>0.0)+1));
  1263. }
  1264.  
  1265. func _multi_times(tmaster, tslave, terror=)
  1266. /* xxDOCUMENT times_list= _multi_times(tmaster, tslave)
  1267.      return the subset of TMASTER which occurs in TSLAVE.  Times in
  1268.      TSLAVE which do not appear in TMASTER are ignored.
  1269.      The optional TERROR keyword determines the accuracy with which
  1270.      TSLAVE must match TMASTER in order to be included.  TERROR may
  1271.      be a scalar, or an array of length TMASTER.  By default TERROR
  1272.      is 0.01 of the spacing between successive elements of TMASTER
  1273.      (see code for the exact expression).
  1274.  */
  1275. {
  1276.   /* handle single points and other common annoying cases */
  1277.   if (numberof(tslave)<1) return [];
  1278.   if (dimsof(tslave)(1)!=1) tslave= tslave(*);
  1279.   if (numberof(tslave)>1 && anyof(tslave(dif)<=0.))
  1280.     tslave= multi_no_dups(tslave(sort(tslave)));
  1281.   if (dimsof(tmaster)(1)!=1) tmaster= tmaster(*);
  1282.   if (numberof(tmaster)<2) {
  1283.     if (numberof(tmaster)<1) return [];
  1284.     if (is_void(terror)) terror= max(1.e-6, 1.e-6*abs(tmaster));
  1285.     if (anyof(abs(tslave-tmaster)<=terror)) return tmaster;
  1286.     else return [];
  1287.   }
  1288.  
  1289.   /* create default terror widths if not supplied */
  1290.   if (is_void(terror)) {
  1291.     dt= tmaster(pcen)(dif);
  1292.     dt(1)*= 2.0;
  1293.     dt(0)*= 2.0;  /* never same as dt(1) since at least two times */
  1294.     terror= 0.01*dt;
  1295.   }
  1296.  
  1297.   /* get list of indices of largest and smallest possible match
  1298.      of tslave in tmaster --
  1299.      the required points are where these are equal */
  1300.   list1= digitize(tslave, tmaster+terror);
  1301.   list2= digitize(tslave, tmaster-terror)-1;
  1302.   list= where(list1==list2);
  1303.   if (numberof(list)) return tmaster(list1(list));
  1304.   else return [];
  1305. }
  1306.  
  1307. /* ------------------------------------------------------------------------ */
  1308.